package router

import (
	"egin/app/controllers"
	"egin/middleware"

	"egin/pkg/config"
	"egin/pkg/gi18n"
	"egin/pkg/logger"
	"egin/pkg/response"
	"egin/pkg/session"
	"net/http"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

//SetRoute
//r 路由配置
func SetRouter(router *gin.Engine) {

	//全局中间件
	router.Use(logger.GinLogger(), logger.GinRecovery(true))
	router.Use(middleware.Cors())
	router.Use(gi18n.I18nMiddleware())
	router.Use(sessions.Sessions(config.C().Session.Name, session.GetStore()))

	router.NoRoute(func(c *gin.Context) {
		c.HTML(http.StatusNotFound, "errors/404.tmpl", nil)
	})

	router.GET("/captcha", controllers.CaptchaImage) //验证码

	admin := router.Group("admin")
	{

		loggedRouter := admin.Group("").Use(middleware.CheckLogged())
		{
			loggedRouter.GET("/login", controllers.LoginHTML) //登陆页面
			loggedRouter.POST("/login", controllers.Login)    //登陆
		}

		app := admin.Group("").Use(middleware.CheckLogin())
		{

			app.GET("/app", controllers.AppHtml)
			app.GET("/appInit", controllers.AppInit)
			app.GET("/logout", controllers.Logout)
			app.GET("/app/setting", controllers.AppSettingHtml)
			app.GET("/app/alertSkin", controllers.AppAlertSkinHtml)
			app.GET("/index", controllers.IndexHtml)
			app.GET("/panel", controllers.PanelHtml)
			app.POST("/unlock", controllers.Unlock)

		}

		role := admin.Group("/role").Use(middleware.CheckLogin())
		{

			role.POST("/xmSelectList", controllers.XmSelectList)
			roleRbac := role.Use(middleware.RbacCheck())
			{
				roleRbac.GET("/index", controllers.RoleIndexHtml)
				roleRbac.GET("/create", controllers.RoleCreateHtml)
				roleRbac.POST("/create", controllers.RoleCreate)
				roleRbac.GET("/edit/:id", controllers.RoleEditHtml)
				roleRbac.PUT("/edit", controllers.RoleEdit)
				roleRbac.POST("/index", controllers.RoleIndex)
				roleRbac.DELETE("/delete/:id", controllers.RoleDelete)
			}

		}

		member := admin.Group("/member").Use(middleware.CheckLogin())
		{
			member.GET("/user_pwd", controllers.MemberUserPwdHtml)
			member.PUT("/user_pwd", controllers.MemberUserPwd)

			member.GET("/user_info", controllers.MemberUserInfoHtml)
			member.PUT("/user_info", controllers.MemberUserInfo)

			memberRbac := member.Use(middleware.RbacCheck())
			{
				memberRbac.GET("/index", controllers.MemberIndexHtml)
				memberRbac.GET("/create", controllers.MemberCreateHtml)
				memberRbac.POST("/create", controllers.MemberCreate)
				memberRbac.GET("/edit/:id", controllers.MemberEditHtml)
				memberRbac.PUT("/edit", controllers.MemberEdit)
				memberRbac.POST("/index", controllers.MemberIndex)
				memberRbac.DELETE("/delete/:id", controllers.MemberDelete)
			}
		}

		permission := admin.Group("/permission").Use(middleware.CheckLogin())
		{
			permission.POST("/permissionRoleTree", controllers.PermissionRoleTree)

			permissionRbac := permission.Use(middleware.RbacCheck())
			{
				permissionRbac.GET("/index", controllers.PermissionIndexHtml)
				permissionRbac.GET("/create", controllers.PermissionCreateHtml)
				permissionRbac.POST("/create", controllers.PermissionCreate)
				permissionRbac.GET("/edit/:id", controllers.PermissionEditHtml)
				permissionRbac.PUT("/edit", controllers.PermissionEdit)
				permissionRbac.POST("/index", controllers.PermissionIndex)
				permissionRbac.DELETE("/delete/:id", controllers.PermissionDelete)
			}

		}
	}

	chat := router.Group("chat")
	{
		chat.GET("ws", func(c *gin.Context) {

			cws := &controllers.Ws{}
			if w, ok := cws.OnOpen(c); ok == true {
				w.OnMessage(c)
			} else {
				response.ErrorWithMessage("websocket open阶段初始化基本参数失败", c)
			}
		})
	}

}
